import { _decorator, Component, Label, log, Node } from 'cc';
import { CommonUtils, MoveDirection } from '../utils/CommonUtils';
import { GlobalRoot } from '../global/GlobalRoot';
import { GlobalManager } from '../global/GlobalManager';
import { AudioManager } from '../managers/AudioManager';
import { UserDate } from '../global/UserDate';
const { ccclass, property } = _decorator;

@ccclass('settingScript')
export class settingScript extends Component {
    allNode: any = {}
    start() {
        this.allNode = CommonUtils.getAllChildrenMap(this.node)
        CommonUtils.moveInDir(this.allNode.bg
            , MoveDirection.Up
            , 0.3, () => {
            })

        console.log(this.allNode.closeBtn)
        CommonUtils.add2DClick(this.allNode.closeBtn, (event) => {
            console.log('closeBtn', event)
            GlobalRoot.instance.UiManager.closePopup(GlobalManager.popups.settingPop)
        })

        this.allNode.Version.getComponent(Label).string = GlobalManager.gameVersion;

        CommonUtils.add2DClick(this.allNode.MusicBtn, () => {
            this.changeMusic()
        })

        CommonUtils.add2DClick(this.allNode.SoundBtn, () => {
            this.changeSound()

        })



        this.changeMusic(true)
        this.changeSound(true)
        CommonUtils.popUp(this.allNode.MusicBtn, 0.3, 0.2)
        CommonUtils.popUp(this.allNode.SoundBtn, 0.3, 0.2)
        CommonUtils.popUp(this.allNode.closeBtn, 0.3, 0.2)

    }

    changeMusic(isInist = false) {
        if (!isInist) {
            UserDate.getInstance().MusicVolume = UserDate.getInstance().MusicVolume > 0 ? 0 : 1
            AudioManager.getInstance().setMusicVolume(UserDate.getInstance().MusicVolume)
        }

        CommonUtils.showChildByName(this.allNode.MusicBtn, UserDate.getInstance().MusicVolume > 0 ? "on" : "off")
    }
    changeSound(isInist = false) {
        if (!isInist) {
            UserDate.getInstance().SoundVolume = UserDate.getInstance().SoundVolume > 0 ? 0 : 1
            AudioManager.getInstance().setEffectVolume(UserDate.getInstance().SoundVolume)
        }
        CommonUtils.showChildByName(this.allNode.SoundBtn, UserDate.getInstance().SoundVolume > 0 ? "on" : "off")

    }


    update(deltaTime: number) {

    }
}


